# Create a new file for topic management
<template>
  <div class="addOrUpdate-block">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="120px"
      class="detail-form-content"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="课程名称" prop="kechengmingcheng">
            <el-input v-model="ruleForm.kechengmingcheng" placeholder="课程名称" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课题名称" prop="ketimingcheng">
            <el-input v-model="ruleForm.ketimingcheng" placeholder="课题名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学期" prop="xueqi">
            <el-input v-model="ruleForm.xueqi" placeholder="学期"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班级" prop="banji">
            <el-input v-model="ruleForm.banji" placeholder="班级"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="作业要求" prop="ketiyaoqiu">
            <el-input type="textarea" v-model="ruleForm.ketiyaoqiu" placeholder="课题要求"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="发布日期" prop="faburiqi">
            <el-date-picker type="date" placeholder="发布日期" v-model="ruleForm.faburiqi" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止时间" prop="jiezhiriqi">
            <el-date-picker type="date" placeholder="截止日期" v-model="ruleForm.jiezhiriqi" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="zhuangtai">
            <el-select v-model="ruleForm.zhuangtai" placeholder="请选择状态">
              <el-option label="未发布" value="未发布"></el-option>
              <el-option label="已发布" value="已发布"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      type: '',
      ruleForm: {
        kechengmingcheng: '',
        ketimingcheng: '',
        xueqi: '',
        banji: '',
        ketiyaoqiu: '',
        faburiqi: '',
        jiezhiriqi: '',
        zhuangtai: '',
        jiaoshigonghao: '',
        jiaoshixingming: ''
      },
      rules: {
        ketimingcheng: [{ required: true, message: '请输入课题名称', trigger: 'blur' }],
        ketiyaoqiu: [{ required: true, message: '请输入课题要求', trigger: 'blur' }],
        faburiqi: [{ required: true, message: '请选择发布日期', trigger: 'change' }],
        jiezhiriqi: [{ required: true, message: '请选择截止日期', trigger: 'change' }],
        zhuangtai: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    };
  },
  props: ["parent"],
  methods: {
    init(id, type) {
      this.id = id;
      this.type = type;
      
      if (this.type == 'cross') {
        var obj = this.$storage.getObj('crossObj');
        for (var key in obj) {
          if (obj[key] && !Array.isArray(obj[key])) {
            this.ruleForm[key] = obj[key];
          }
        }
      }
      
      // 获取用户信息
      this.$http({
        url: `${this.$storage.get('sessionTable')}/session`,
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          var json = data.data;
          if (json.jiaoshigonghao != null && json.jiaoshigonghao != '') {
            this.ruleForm.jiaoshigonghao = json.jiaoshigonghao;
            this.ruleForm.jiaoshixingming = json.jiaoshixingming;
          }
        }
      });
    },
    onSubmit() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: `ketibuzhi/${!this.ruleForm.id ? "save" : "update"}`,
            method: "post",
            data: this.ruleForm
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.parent.showFlag = true;
                  this.parent.addOrUpdateFlag = false;
                  this.parent.ketiBuzhiCrossAddOrUpdateFlag = false;
                  this.parent.search();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    back() {
      this.parent.showFlag = true;
      this.parent.addOrUpdateFlag = false;
      this.parent.ketiBuzhiCrossAddOrUpdateFlag = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.detail-form-content {
  padding: 20px;
}
</style> 